package com.kd.xjoa.scheduled.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kd.xjoa.scheduled.domain.KDDictionary;
import com.kd.xjoa.scheduled.domain.LedgerAssign;
import com.kd.xjoa.scheduled.domain.SumCompCheckDaily;
import com.kd.xjoa.scheduled.mapper.DictionaryMapper;
import com.kd.xjoa.scheduled.mapper.SumCheckDailyMapper;
import com.kd.xjoa.scheduled.search.bean.IndexParam;
import com.kd.xjoa.scheduled.service.BussinessService;
import com.kd.xjoa.scheduled.utils.ScheduledCommonUtil;
import com.kd.yq.common.utils.CommonUtils;

@Service
public class BussinessServiceImpl implements BussinessService {

	private static final Log logger = LogFactory.getLog(BussinessServiceImpl.class);

	@Autowired
	private SumCheckDailyMapper sumCheckDailyMapper;
	
	@Autowired
	private DictionaryMapper dictionaryMapper;
	
	private static final String DIC_LEDGER_YPE="db_oltp.tb_trs_ledger_assign.ledger_type";
	
	@Override
	public Integer totalBussinessMonitor(String snapDate) {
		SumCompCheckDaily sumCompCheckDaily = null;
		StringBuffer sourceSb = null;
		List<Integer> sourceList = null;
		IndexParam param = null;
		int result = 0;
		int totalCounts = 0;
		//记录非网站数据的数量
		int otherCounts = 0;
		List<SumCompCheckDaily> list = new ArrayList<SumCompCheckDaily>();
		try {
			List<KDDictionary> dicList = dictionaryMapper.getDictionaryByColName(DIC_LEDGER_YPE);
			if(CommonUtils.isBlank(dicList)){
				return result;
			}
			List<LedgerAssign> assignList = sumCheckDailyMapper.listLedgerAssign();
			if(CommonUtils.isBlank(assignList)){
				return result;
			}
			for (KDDictionary dictionary : dicList) {
				//网源类型数据字典中网站的值为14
				if(dictionary == null || StringUtils.isBlank(dictionary.getCol_key()) || "14".equals(dictionary.getCol_key())){
					continue;
				}
				sourceList = new ArrayList<Integer>();
				for (LedgerAssign ledgerAssign : assignList) {
					if(ledgerAssign == null || ledgerAssign.getLedger_type() == null){
						continue;
					}
					if(ledgerAssign.getLedger_type().intValue() == Integer.valueOf(dictionary.getCol_key()).intValue()){
						sourceList.add(ledgerAssign.getLedger_id());
					}
				}
				sourceSb = new StringBuffer();
				for (Integer source : sourceList) {
					sourceSb.append(source.toString()).append(",");
				}
				if(sourceSb.length() > 0){
					sourceSb.deleteCharAt(sourceSb.length()-1);
				}
				param = new IndexParam();
				param.setSource(sourceSb.toString());
				ScheduledCommonUtil.initDateTime(param, snapDate);
				//统计总数
				totalCounts = ScheduledCommonUtil.indexSearch(param);
				//返回结构为-1说明索引调用失败
				if(totalCounts == -1){
					logger.error("sourceType:"+dictionary.getCol_value()+",index search error, result null ");
					continue;
				}
				logger.info("sourceType:"+dictionary.getCol_value()+", result counts:"+totalCounts);
				
				sumCompCheckDaily = new SumCompCheckDaily();
				sumCompCheckDaily.setSnap_date(Integer.valueOf(snapDate));
				sumCompCheckDaily.setComp_id("comp000000000000");
				//看郭经理提供的shell脚本，专题设置的为1，来源类型（fb、wx、wb等）设置的为2
				sumCompCheckDaily.setCheck_item_group("2");
				sumCompCheckDaily.setCheck_item(dictionary.getCol_value());
				sumCompCheckDaily.setTotal_amt(totalCounts);
				list.add(sumCompCheckDaily);
				
				otherCounts += totalCounts;
			}
			//查询只传时间参数的索引数据
			IndexParam allParam = new IndexParam();
			ScheduledCommonUtil.initDateTime(allParam, snapDate);
			int nowAllCounts = ScheduledCommonUtil.indexSearch(allParam);
			if(nowAllCounts != -1 && nowAllCounts >= otherCounts){
				SumCompCheckDaily otherSum = new SumCompCheckDaily();
				otherSum.setSnap_date(Integer.valueOf(snapDate));
				otherSum.setComp_id("comp000000000000");
				//看郭经理提供的shell脚本，专题设置的为1，来源类型（fb、wx、wb等）设置的为2
				otherSum.setCheck_item_group("2");
				//网站数据字典中的值为14
				otherSum.setCheck_item("网站");
				//网站的数量=所有数量-平台数量
				otherSum.setTotal_amt(nowAllCounts-otherCounts);
				list.add(otherSum);
			}
		} catch (Exception e) {
			logger.info("totalBussinessTypeMonitor error ");
			e.printStackTrace();
		}finally{
			if(CommonUtils.isNotBlank(list)){
				result = sumCheckDailyMapper.batchAddSumCompCheckDaily(list);
				logger.info("totalBussinessTypeMonitor, repalce into success,counts: "+result);
			}
		}
		
		return result;
	}

}
